package com.wectrls.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wectrls.common.CommonVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019-11-04 15:48
 */
@RestController
@RequestMapping(value = "/file")
public class FileUploadController {

    @PostMapping(value = "/upload")
    public Object uploadHtml(@RequestParam("file") MultipartFile file) {
        InputStream inputStream;
        BufferedReader reader = null;
        List<Bookmark> bookmarks = Lists.newArrayList();
        Map<String, List<Bookmark>> bookmarkMap = Maps.newLinkedHashMap();
        try {
            inputStream = file.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            reader = new BufferedReader(inputStreamReader);
            String line;
            StringBuffer buffer = new StringBuffer("");
            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
                buffer = buffer.append(line.trim());
            }
            System.out.println(buffer);
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(file.getOriginalFilename());

        return new CommonVO<>("ok");
    }
}
